package fr.efrei.tradesys.hibernate.daoImpl;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

import fr.efrei.tradesys.hibernate.HibernateUtil;
import fr.efrei.tradesys.hibernate.beans.Currency;
import fr.efrei.tradesys.hibernate.dao.CurrencyDao;

public class CurrencyDaoImpl implements CurrencyDao
{

	private static CurrencyDaoImpl	instance	= new CurrencyDaoImpl();

	public static CurrencyDaoImpl getInstance()
	{
		return instance;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Currency> getAllCurrency()
	{
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		Criteria criteria = session.createCriteria(Currency.class);
		List<Currency> list = (List<Currency>) criteria.list();
		return list;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Currency getCurrency(String code)
	{
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		Criteria criteria = session.createCriteria(Currency.class).add(
				Restrictions.eq("code", code));
		List<Currency> list = (List<Currency>) criteria.list();
		if (list.size() > 0)
			return list.get(0);
		return null;
	}

	@Override
	public void addCurrency(Currency c)
	{
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.saveOrUpdate(c);
	}

	@Override
	public void updateCurrency(Currency c)
	{
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.saveOrUpdate(c);
	}

	@Override
	public void removeCurrency(Currency c)
	{
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.delete(c);
	}

}
